package com.scau.clothes.stock.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scau.clothes.core.util.CheckObjectIsNullUtils;
import com.scau.clothes.stock.mapper.ColorMapper;
import com.scau.clothes.stock.mapper.NormMapper;
import com.scau.clothes.stock.mapper.SizeMapper;
import com.scau.clothes.stock.model.entity.Color;
import com.scau.clothes.stock.model.entity.Norm;
import com.scau.clothes.stock.model.entity.Size;
import com.scau.clothes.stock.service.NormService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class NormServiceImpl extends ServiceImpl<NormMapper, Norm> implements NormService {
    @Resource
    private NormMapper normMapper;
    @Resource
    private ColorMapper colorMapper;
    @Resource
    private SizeMapper sizeMapper;
    private CheckObjectIsNullUtils checkObjectIsNullUtils = new CheckObjectIsNullUtils();
    @Override
    public int insertNorm(Norm norm) {
        norm.setNormSn("n" + norm.getColorId().toString() + norm.getSizeId().toString());
        norm.setIsDelete(0);
        return normMapper.insert(norm);
    }

    @Override
    public int updateNorm(Norm norm) {
        norm.setNormSn("n" + norm.getColorId().toString() + norm.getSizeId().toString());
        return normMapper.updateById(norm);
    }

    @Override
    public int deleteNorm(Norm norm) {
        norm.setIsDelete(1);
        return normMapper.updateById(norm);
    }

    @Override
    public List<Norm> listNormByColorAndSize(Norm norm) {
        QueryWrapper<Norm> normQueryWrapper=new QueryWrapper<>();
        normQueryWrapper.ne("is_delete", 1);
        if(!checkObjectIsNullUtils.objCheckIsNull(norm)){
            if (norm.getColorId() != null)
                normQueryWrapper.eq("color_id",norm.getColorId());
            if (norm.getSizeId() != null)
                normQueryWrapper.eq("size_id",norm.getSizeId());
        }
        List<Norm> list=list(normQueryWrapper);
        for (Norm n:
                list) {
            Color c = new Color();
            Size s = new Size();
            if (n.getColorId() != null)
                c = colorMapper.selectById(n.getColorId());
            if (n.getSizeId() != null)
                s  = sizeMapper.selectById(n.getSizeId());
            if (!checkObjectIsNullUtils.objCheckIsNull(c) && c.getIsDelete() != 1)
                n.setColor(c);
            if (!checkObjectIsNullUtils.objCheckIsNull(s) && s.getIsDelete() != 1)
                n.setSize(s);
        }
        return list;
    }
    @Override
    public Norm selectNormByNormSn(String normSn) {
        QueryWrapper<Norm> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("norm_sn",normSn);
        Norm norm = getOne(queryWrapper);
        if (norm.getIsDelete() != 1)
            return norm;
        return null;
    }
}
